﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Outils;
using FichierTxt;
using projet_fichier_objet;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace App_Recherche
{
    public partial class _Default : System.Web.UI.Page
    {
        List<Personne> ListInterimaire = new List<Personne>();

        protected void Page_Load(object sender, EventArgs e)
        {
            /*Rappel sur le page load :
             * 
             * 
             * Le page load se charge à chaque appel de page 
             */
            //(List<Personne>)Session["Liste"];
            NoResult.Visible = false;
            string id = Request.QueryString["id"];
            string Nbligne = Request.QueryString["Aff"];
            int ID = 0;
            if (int.TryParse(id, out ID)) //convertion en entier
            {
                if (ID == (int)Utils.TypePage.edition) //page édition
                {
                    messagereussite.Text = " Edition Réussite," + Nbligne + " ligne(s) modifiée(s)";
                }
                else if (ID == (int)Utils.TypePage.ajout) //page ajout
                {
                    messagereussite.Text = " Intérimaire Ajouté ";
                }
                else if (ID == (int)Utils.TypePage.contact)//contact
                {
                    messagereussite.Text = " Email Envoyé ";
                }
                else if (ID == (int)Utils.TypePage.suppression)
                {
                    messagereussite.Text = " Suppression réussite ";
                }
                else //si premier postback
                    messagereussite.Text = string.Empty;
            }
        }

        protected void submit_Click(object sender, EventArgs e)
        {
            bool erreur = Utils.CheckSaisi(Nom.Text); //vérification du nom entré
            MessageErreur.Text = string.Empty;
            if (erreur == false)
            {
                bool hasResults = false;
                //affecte la methode rptPersonnes_ItemDataBound lorsque l'on va boucler sur les données

                //string CnxInterimaire = "Data Source=PILOTT-LGZ;Initial Catalog=Interimaires;Integrated Security=True;Pooling=False";
                SqlConnection SqlC = new SqlConnection(Utils.CnxInterimaire);
                SqlC.Open();
                SqlCommand SQLCmd = SqlC.CreateCommand();
                int CodeAg = 0;
                int Recherche = 0;
                if (int.TryParse(ListeAgence.SelectedValue, out CodeAg))
                {
                    if (int.TryParse(Radiobutton2.SelectedValue, out Recherche))
                    {
                        if (CodeAg != 0)
                        {
                            if (Recherche == 1)
                            {
                                SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Agence = '" + ListeAgence.SelectedValue + "' AND Nom LIKE '" + Nom.Text + "%'";
                            }
                            else if (Recherche == 2)
                            {
                                SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Agence = '" + ListeAgence.SelectedValue + "' AND Nom LIKE '%" + Nom.Text + "'";
                            }
                            else if (Recherche == 3)
                            {
                                SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Agence = '" + ListeAgence.SelectedValue + "' AND Nom LIKE '%" + Nom.Text + "%'";
                            }
                            else
                                SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Agence = '" + ListeAgence.SelectedValue + "' AND Nom = '" + Nom.Text + "'";
                        }
                        else
                        {
                            if (Recherche == 1)
                            {
                                SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Nom LIKE '" + Nom.Text + "%'";
                            }
                            else if (Recherche == 2)
                            {
                                SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Nom LIKE '%" + Nom.Text + "'";
                            }
                            else if (Recherche == 3)
                            {
                                SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Nom LIKE '%" + Nom.Text + "%'";
                            }
                            else
                            {
                                SQLCmd.CommandText = " SELECT * FROM Interimaire WHERE Nom = '" + Nom.Text + "'";
                            }
                        }
                    }
                }

                SqlDataReader SqlReader = SQLCmd.ExecuteReader();
                while (SqlReader.Read() && SqlReader != null)
                {
                    Personne candidat = new Personne();

                    int ID = 0;
                    if (int.TryParse(SqlReader["Id"].ToString(), out ID))
                    {
                        candidat.ID = ID;
                    }
                    candidat.Nom = SqlReader["Nom"].ToString();
                    candidat.Email = SqlReader["Email"].ToString();
                    candidat.Ville = SqlReader["Ville"].ToString();
                    candidat.DateNaissance = Convert.ToDateTime(SqlReader["DateNaiss"]);
                    candidat.CodeAgence = (int)SqlReader["Agence"];
                    hasResults = true;

                    ListInterimaire.Add(candidat);
                }
                SqlC.Close();

                if (hasResults)
                {
                    Session["Liste"] = ListInterimaire;

                }
                else
                {
                    NoResult.Visible = true;
                }

                //Response.Redirect("~/resultat.aspx?Nom=" + Nom.Text + "&Recherche=" + Radiobutton2.SelectedValue + "&Ag=" + ListeAgence.SelectedValue);
            }
            else
                MessageErreur.Text = "Erreur saisie incorrecte";

        }

        protected void reset_Click(object sender, EventArgs e)
        {
            //remise à zero des textbox
            Nom.Text = string.Empty;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //redirection veers la page affichage
            Response.Redirect("~/Page_aspx/Affichage.aspx?Ag=" + ListeAgence.SelectedValue);
        }

        protected void Radiobutton2_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }

        protected void btContact_Click(object sender, EventArgs e)
        {
        }

        protected void Unnamed1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            rptPersonnes.Personnes = ListInterimaire;
            if (ListInterimaire.Count != 0)
            {
                rptPersonnes.Visible = true;
            }
            else
                rptPersonnes.Visible = false;
        }
    }

    public static class Actions
    {
        public static string TypeAction = "type";
    }
}
